package servlet.register;

import pojo.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/RegisterByEmailServlet",name = "RegisterByEmailServlet")
public class RegisterByEmailServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String loginName = request.getParameter("login_name");
        String password = request.getParameter("password");

        String email = request.getParameter("email");

        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setStatus(0); //未激活状态
        user.setUsername(loginName);
        user.setEmail(email);
        if(userService.registerByEmail(user)){
            response.getWriter().print("注册成功！用户名："+loginName+", 密码："+password+",请登录邮箱进行验证(5分钟有效)："+email);
        } else {
            response.getWriter().print("注册失败！");
        }

    }
}
